package net.lucamasini.ricette.client;

import java.util.logging.Level;
import java.util.logging.Logger;

import net.lucamasini.ricette.client.ricette.*;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.UmbrellaException;
import com.google.gwt.place.shared.*;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class IndovinaChiVieneACena extends Composite implements EntryPoint {

	private final static Logger LOG = Logger.getLogger(IndovinaChiVieneACena.class.getName());
	
	private static IndovinaChiVieneACenaUiBinder uiBinder = GWT.create(IndovinaChiVieneACenaUiBinder.class);
	private ClientFactory clientFactory = GWT.create(ClientFactory.class);
	
	@UiField
	TabLayoutPanel tabLayoutPanel;
	
	@UiField(provided=true)
	Ricette ricette = (Ricette)clientFactory.getRicetteView();
	
	interface IndovinaChiVieneACenaUiBinder extends UiBinder<DockLayoutPanel, IndovinaChiVieneACena> {
	}
	
	RicettarioRequestFactory requestFactory = GWT.create(RicettarioRequestFactory.class);
	
	private final ActivityMapper mapper = new ActivityMapper() {
		@SuppressWarnings("unchecked")
		@Override
		public Activity getActivity(Place place) {
			if( place instanceof RicettePlace ) {
				return new RicetteActivity(clientFactory);	
			} else if( place instanceof ProxyPlace ) {
				ProxyPlace proxyPlace = (ProxyPlace) place;
				if( proxyPlace.getProxyClass()==RicettaProxy.class ) {
					return new RicetteDialogActivity((EntityProxyId<RicettaProxy>)proxyPlace.getProxyId(), clientFactory);	
				} else {
					return null;
				}
			} else {
				return null;
			}
		}
	};

	@WithTokenizers({RicettePlace.Tokenizer.class})
	public interface AppPlaceHistoryMapper extends PlaceHistoryMapperWithFactory<TokenizerFactory>
	{
	}
	
	public class TokenizerFactory {
		public ProxyPlace.Tokenizer factory() {
			return new ProxyPlace.Tokenizer(clientFactory.getRequestFactory());
		}
		
	}
	
	private final AppPlaceHistoryMapper historyMapper= GWT.create(AppPlaceHistoryMapper.class);
	
	private final ActivityManager activityManager = new ActivityManager(mapper, clientFactory.getEventBus());
	 
	public void onModuleLoad() {

		final Widget createAndBindUi = uiBinder.createAndBindUi(this);

		GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
			
			@Override
			public void onUncaughtException(Throwable e) {
				
				if(e instanceof UmbrellaException) {
					UmbrellaException ue = (UmbrellaException) e;
					for(Throwable t: ue.getCauses()) {
						LOG.log(Level.SEVERE, t.getMessage(), t);
						Window.alert(t.getMessage());
					}
				} else {
					LOG.log(Level.SEVERE, e.getMessage(), e);
					Window.alert(e.getMessage());
				}
				
			}
		});
		
		ServiceBus.registerEventHandlers();
		
		activityManager.setDisplay(new AcceptsOneWidget() {
			@Override
			public void setWidget(IsWidget w) {
				if( w!=null ) {
					tabLayoutPanel.selectTab(w);	
				}
			}
		});
		
		historyMapper.setFactory(new TokenizerFactory());
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
        historyHandler.register(clientFactory.getPlaceController(), clientFactory.getEventBus(), new RicettePlace());
        
        RootLayoutPanel.get().add(createAndBindUi);
        
        // Goes to the place represented on URL else default place
        historyHandler.handleCurrentHistory();
	}
}
